/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.general;

import jade.core.AID;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gnaskky
 */

public class GetCHListBehaviour extends AbstractListenOnceBehaviour {

    private BaseAgent agent;

    public GetCHListBehaviour(BaseAgent ag) throws Exception {
        this.agent = ag;
        //mt = MessageTemplate.MatchPerformative(BaseMessage.NOTIFY)

        // sends request for CH list
        ACLMessage out_msg = new ACLMessage(ACLMessage.REQUEST);
        out_msg.addUserDefinedParameter(Config.IM_MSG_TYPE, Config.GET_CHS_MSG);
        out_msg.addReceiver(new AID(Config.EVENT_SERVICE_NAME, AID.ISLOCALNAME));
        agent.sendMessage(out_msg);

        // installs a mask for listening
        ACLMessage wrap_msg = new ACLMessage( ACLMessage.CONFIRM );
        wrap_msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.POST_CHS_MSG);
        mt = MessageTemplate.MatchCustom(wrap_msg, true);
    }

    @Override
    public void handleMessage( ACLMessage in_msg ) {
        try {
            //fetches the ch vector :)
            agent.setCHs((Hashtable<String,Double>) in_msg.getContentObject());

            if(Config.TRACE_LVL > Config.ULTRA_TR_LVL )
                System.out.println("[GetCHListBehaviour]\t Obtainded CH_list\t\t" + agent.getLocalName() );

            this.setDone();

        } catch (UnreadableException ex) {
            Logger.getLogger(GetCHListBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

}
